package com.jshongli.myapplication.login;

import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.jshongli.myapplication.R;
import com.jshongli.myapplication.base.BaseActivity;
import com.jshongli.myapplication.common.callback.UpdateCallback;
import com.jshongli.myapplication.common.utils.AccountManager;
import com.jshongli.myapplication.common.utils.AppManager;
import com.jshongli.myapplication.common.utils.SuccessDialogUtils;
import com.jshongli.myapplication.common.utils.ToastUtils;
import com.jshongli.myapplication.common.utils.URLUtils;
import com.jshongli.myapplication.common.utils.UtilPreferences;
import com.jshongli.myapplication.model.base.Update;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;

public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.et_oldpass)
    EditText et_oldpass;

    @BindView(R.id.et_newpass)
    EditText et_newpass;

    @BindView(R.id.et_confirm)
    EditText et_confirm;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initView() {
        super.initView();

        mToolbar.setTitle("修改密码");
        initToolbarBack(mToolbar);

        initPD();
    }

    //region initPD
    private ProgressDialog pd;

    private void initPD() {
        pd = new ProgressDialog(mContext);
        pd.setCanceledOnTouchOutside(false);
        pd.setCancelable(false);
        pd.setMessage(getString(R.string.waiting));
    }
    //endregion

    @Override
    protected void initData() {
        super.initData();

    }

    public void changepwd(String newpwd , String oldpwd) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", UtilPreferences.getString(mContext,"userid"));//
        map.put("oldPassword", oldpwd);
        map.put("newPassword", newpwd);
        Gson gson = new Gson();
        final String s = gson.toJson(map);
        String url = URLUtils.URLNoToken("manageAPI/sysUser/editPassword");
        OkHttpUtils
                .postString()
                .url(url)
                .addHeader("Authorization", AccountManager.getToken())
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(s)
                .build()
                .execute(new UpdateCallback<Update>(mContext) {

                    @Override
                    public void onSuccess(Update response) {
                        pd.dismiss();
                        if (response.success)
                        {
                            ToastUtils.show(mContext, "修改成功，请重新登录");
                            UtilPreferences.removeKey(mContext, getResources().getString(R.string.token));
                            UtilPreferences.removeKey(mContext, getResources().getString(R.string.pwd));
                            AppManager.finishAllActivity();
                            openActivity(LoginActivity.class, true);
                        }else {
                            ToastUtils.show(mContext, "修改失败,请重试");
                        }
                    }

                    @Override
                    public void onUpdating() {

                    }

                    @Override
                    public void onUpdateFailed() {
                        pd.dismiss();
                        ToastUtils.show(mContext, "与服务器连接失败,请重试");
                    }
                });
    }

    @OnClick({R.id.btn_complete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_complete:
                pd.show();
                String newpass = et_newpass.getText().toString().trim();
                String confirm = et_confirm.getText().toString().trim();
                String oldpass = et_oldpass.getText().toString().trim();
                String pass = UtilPreferences.getString(mContext, "pwd");
                if (TextUtils.isEmpty(oldpass)) {
                    pd.dismiss();
                    ToastUtils.show(mContext, "请输入旧密码。");
                    return;
                }
                if (!pass.equals(oldpass)) {
                    pd.dismiss();
                    ToastUtils.show(mContext, "请输入正确的旧密码。");
                    return;
                }
                if (TextUtils.isEmpty(newpass)) {
                    pd.dismiss();
                    ToastUtils.show(mContext, "请输入新密码。");
                    return;
                }
                if (TextUtils.isEmpty(confirm)) {
                    pd.dismiss();
                    ToastUtils.show(mContext, "请输入确认密码。");
                    return;
                }
                if (!newpass.equals(confirm)) {
                    pd.dismiss();
                    ToastUtils.show(mContext, "两次填写的密码不一致。");
                    return;
                }
                changepwd(newpass,oldpass);
                break;

        }
    }
}